<?PHP
// vim: set expandtab tabstop=4 shiftwidth=4:
// +----------------------------------------------------------------------+
// | SAPID: XML Sapiens Engine Demonstrator                               |
// +----------------------------------------------------------------------+
// | Author:  Pavel Levkovich <pl@rg.by>								  |
// |          Max Baryshnikov aka Mephius <mb@rg.by>	                  |
// | Copyright (c) 2004 Max Baryshnikov                                   |
// | http://sapid.sourceforge.net	                                      |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+
// Release: 06.12.04 (dd/mm/yy)
// $Id: tree.js.php,v 1.9 2006/02/20 08:39:46 sheiko Exp $

// mod toxin I've changed the prolog in index.php to html valid 4.01 (ref here> http://gutfeldt.ch/matthias/articles/doctypeswitch/table.html) because of this I've removed the width:100% from 262 row: style attrib (Under IE the tree table has became  incorrect)

include("../../../etc/rc.conf.php");
include("../../../lang/".$default_language.".inc.php");

if($MODREWRITE == "MultiviewsMode") 
	$http_path .= "index.php/";

@ini_set("url_rewriter.tags", ""); //Tell the server not to insert anything to this code

session_id($_GET["sid"]);
session_start();
ob_start();
?>
//<script>
img_line		= new Image(); 	img_line.src 		= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/line.gif";
img_minus		= new Image(); 	img_minus.src		= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/minus.gif";
img_minusbottom	= new Image(); 	img_minusbottom.src	= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/minusbottom.gif";
img_plus		= new Image(); 	img_plus.src		= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/plus.gif";
img_plusbottom	= new Image(); 	img_plusbottom.src	= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/plusbottom.gif";
img_join		= new Image(); 	img_join.src		= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/join.gif";
img_joinbottom	= new Image(); 	img_joinbottom.src	= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/joinbottom.gif";
recycle_empty	= new Image();	recycle_empty.src 	= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/recycle_empty.gif";
recycle_full	= new Image();	recycle_full.src 	= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/recycle_full.gif";
img_page_o 		= new Image();	img_page_o.src		= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/o.gif";
img_page_p 		= new Image();	img_page_p.src		= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/p.gif";
img_page_f 		= new Image();	img_page_f.src		= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/f.gif";
img_page_o02 	= new Image();	img_page_o02.src	= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/o02.gif";
img_page_p02 	= new Image();	img_page_p02.src	= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/p02.gif";
img_page_f02 	= new Image();	img_page_f02.src	= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/f02.gif";
x		 		= new Image();	x.src				= "<?=$GLOBALS["env"]["http_path"]?>usr/system/images/tree/x.gif";


up 			= "<span style='cursor: default; padding-top:1px; font-size: 12px; width:10px; text-decoration: none; text-align: center' title='Move Up'>&#9650;</span>";
down 		= "<span style='cursor: default; padding-top:1px; font-size: 12px; width:10px; text-decoration: none; text-align: center' title='Move Down'>&#9660;</span>";
line 		= "<img src=" + img_line.src + " width=17 height=21 border=0 align='absmiddle' />";
empty 		= "<span style='width: 17px'><img src=" + x.src + " width=17 height=1 / ></span>";
minus 		= "<img src=" + img_minus.src + " width=17 height=21 border=0 align='absmiddle' class='nonoel' />";
minusbottom = "<img src=" + img_minusbottom.src + " width=17 height=21 border=0 align='absmiddle' class='nonoel' />";
plus 		= "<img src=" + img_plus.src + " width=17 height=21 border=0 align='absmiddle' class='nonoel' />";
plusbottom 	= "<img src=" + img_plusbottom.src + " width=17 height=21 border=0 align='absmiddle' class='nonoel' />";
join 		= "<img src=" + img_join.src + " width=17 height=21 border=0 align='absmiddle' class='nonoel' />";
joinbottom 	= "<img src=" + img_joinbottom.src + " width=17 height=21 border=0 align='absmiddle' class='nonoel' />";
hline = "<span style='margin:0px 2px 2px 2px; color: #0277ED; font-size: 12px'>I</span>";

var dragID = false;
var isMozilla=false;
var isIE=true;
var Last_New_Id=0;
var Last_Pointer_Id=0;
var max_id=0;
var curnodeID="";
var in_props = false;
var id_array=new Array();
var BackColor = "#C2DCED";
var BackColorOver = "#B5D1E4";
var User = "<?=$_SESSION["user"]["LOGIN"]?>";
var Group = "<?=$_SESSION["user"]["GROUP"]?>";
var pRead=false;
var pWrite=false;
var pCreate=false;
var drag=false;
var menu=false;
menu=document.getElementById('menu');

function reportParseError(error){
	error_str  = "<H4>Error of document loading '" +      error.url + "'</H4>" +
	"<p><font color='red'>" + error.reason +     "</font></p>";
	if (error.line > 0)
	error_str += "<H5>" +    "Line " + error.line + ", symbol " + error.linepos +
	"\n" + error.srcText +   "</H4>";
	return error_str;
}

function init(xmlfilepath){
	document.getElementById('sChanges').disabled=true;
	document.getElementById('sInit').disabled=true;
	document.getElementById('RESULTS').innerHTML='Tree status: loading data...';
	recylceFull=false;
	result_str = "";
	if(document.implementation && document.implementation.createDocument){
		docobj=document.implementation.createDocument("","doc", null);

		//Emulate IE xml property

		XMLDocument.prototype.__defineGetter__("xml", function (){ return (new XMLSerializer()).serializeToString(this); });
		Node.prototype.__defineGetter__("xml", function (){	return (new XMLSerializer()).serializeToString(this); });

		//Emulate IE text property

		XMLDocument.prototype.__defineGetter__("text", function (){ return this.textContent; });
		XMLDocument.prototype.__defineSetter__("text", function (text){ this.textContent=text; });
		Node.prototype.__defineSetter__("text", function (text){ this.textContent=text; });
		Node.prototype.__defineGetter__("text", function (){	return this.textContent });


		//Emulate IE loadXML method

		XMLDocument.prototype._clearDOM = function()
		{
			while(this.hasChildNodes())
			this.removeChild(this.firstChild);
		}



		XMLDocument.prototype._copyDOM = function(oDoc)
		{
			this._clearDOM();

			// importNode is not yet needed in Moz due to a bug but it will be
			// fixed so...

			var oNodes = oDoc.childNodes;
			for(i=0;i<oNodes.length;i++)
			this.appendChild(this.importNode(oNodes[i], true));
		};


		XMLDocument.prototype.loadXML = function(strXML)
		{
			var sOldXML = this.xml;
			var oDoc = (new DOMParser()).parseFromString(strXML, "text/xml");
			this._copyDOM(oDoc);
			return sOldXML;
		};


		//Emulate IE NodeFromID() method

		XMLDocument.prototype.nodeFromID = function(id)
		{
			return this.getElementById(id);
		}



		isMozilla=true;
		isIE=false;
	}else{
		docobj = new ActiveXObject("Msxml2.DOMDocument");
	}

	if(isMozilla) docobj.async=false; else docobj.onreadystatechange = CheckState;
	//docobj.async=false;

	docobj.resolveExternals = false;
	docobj.load(xmlfilepath);
	if(isMozilla) {
		docobj.normalize();
		printElements();
	}
	if (docobj.parseError && docobj.parseError.errorCode != 0){
		xmlTree.innerHTML=reportParseError(docobj.parseError);
		return;
	}
}

function CheckState(){
	var state = docobj.readyState;
	document.getElementById('RESULTS').innerHTML = "Tree status: " + (state/4)*100 + "% loaded";
	if (state == 4){
		var err = docobj.parseError;
		if (err.errorCode != 0){
			document.getElementById('RESULTS').innerHTML = err.reason + " ";
		}else{
			document.getElementById('RESULTS').innerHTML = "Tree status: printing elements... ";
			setTimeout(printElements, 10);
		}
	}
}

function getMaxId(curNode){
	var curID = (curNode.attributes.getNamedItem('id'))?(isMozilla?curNode.attributes.getNamedItem('id').textContent:curNode.attributes.getNamedItem('id').text):"";
	var childs = curNode.childNodes;
	var childnodes = curNode.childNodes.length;
	if(parseInt(curID.substr(3))>max_id) max_id=parseInt(curID.substr(3));
	for(var i=0;i<childnodes;i++) if (childs.item(i).nodeName=="treeitem") getMaxId(childs.item(i));
}

function printElements(){
	levels = new Array()
	for(i=0; i<20; i++)
	{levels[i]=0;}
	result_str ="";
	level=0 ;
	for(i=0;i<docobj.documentElement.childNodes.length;i++)
	if(docobj.documentElement.childNodes.item(i).nodeName=='treeitem') {
		getMaxId(docobj.documentElement.childNodes.item(i));
		//document.getElementById('RESULTS').innerHTML = "";

		viewNode(docobj.documentElement.childNodes.item(i));
	}
	var	xmlTree=document.getElementById('xmlTree');
	xmlTree.innerHTML = result_str;
	xmlTree.innerHTML = xmlTree.innerHTML + '<div style="padding-left: 15px; background: '+BackColor+';"><img id="recycle" src="'+recycle_empty.src+'" width=35 height=35 border=0 align="absmiddle" class="nonoel" alt="Empty" '+(isMozilla?'onmouseup="dropTrash()"':'ondragover="window.event.returnValue=false;" ondrag="window.event.returnValue=false;" ondrop="dropTrash()"')+'><input type="hidden" id="recylceId" name="recylceId" value="" /></div>';
	xmlTree.innerHTML =  xmlTree.innerHTML + '<textarea id="new_xml" cols=110 rows=10 name="post_site[new_xml]" style="display: none;"></textarea>';
	document.getElementById("new_xml").value=docobj.xml;
	if (recylceFull) {
		document.getElementById("recycle").src=recycle_full.src;
		document.getElementById("recycle").alt='Recycle Bin Full'+" "+recylceFull;
		document.getElementById("recylceId").value=recylceFull;
	}
	document.getElementById('RESULTS').innerHTML = "Tree status: ready";
	document.getElementById('sChanges').disabled=false;
	document.getElementById('sInit').disabled=false;

}


//выдает строку, содержащую дерево с элемента curNode

function viewNode(curNode){
	curNode.normalize();
	var childs = curNode.childNodes;
	if(isMozilla && curNode.nextSibling && curNode.nextSibling.nodeName=="treeitem") var lastNode=true; else
	var lastNode = (isMozilla?!!(curNode.nextSibling && curNode.nextSibling.nodeName=="#text" && curNode.nextSibling.nextSibling):(curNode.nextSibling != null));



	var childnodes = childs.length;
	var curID = (curNode.attributes.getNamedItem('id'))?(isMozilla?curNode.attributes.getNamedItem('id').textContent:curNode.attributes.getNamedItem('id').text):"";
	var hasChildren = false;
	// Если не задан visible

	if (!curNode.attributes.getNamedItem('visible')) curNode.setAttribute('visible','true');
	var visible=(isMozilla?curNode.attributes.getNamedItem('visible').textContent:curNode.attributes.getNamedItem('visible').text);

	if (curNode.nodeName=="treeitem"){
		childsActions=true;

		checkPermissions(curNode.parentNode);
		ppRead=pRead;
		ppWrite=pWrite;
		ppCreate=pCreate;
		checkPermissions(curNode);

		result_str+="<div class=table id=\"tab_"+curID+"\" >";
		result_str+="<table border='0' cellspacing='0' cellpadding='0'><tr onMouseOver=\"this.style.backgroundColor='"+BackColorOver+"'; document.getElementById('div_"+curID+"').style.backgroundColor='"+BackColorOver+"';\" onMouseOut=\"this.style.backgroundColor='"+BackColor+"'; document.getElementById('div_"+curID+"').style.backgroundColor='"+BackColor+"';\" style=\"background-color: "+BackColor+"\">";
		result_str+="<td width=\"100%\" nowrap id="+curID+" "+((pCreate && level!=0)?"oncontextmenu=\"return showmenu('"+curID+"', event);\"":"")+"><div id=\"div_"+curID+"\"style=\"float: left; background-color: "+BackColor+"\">";
		if(curNode.getAttribute('permissions')=="755") {
			if(pCreate) folderopen = (isMozilla?"<img src='"+img_page_o.src+"' onmousedown=\"MydragStart('"+curID+"', event);\" onmouseup=\"MyDragEnd('"+curID+"', event);\" width='17' height='21' border='0' align='absmiddle' />":"<img ondrag=\"dragobj()\" ondragover=\"window.event.returnValue=false;\" ondrop=\"MyDragEnd('"+curID+"')\" ondragstart=\"MydragStart('"+curID+"')\" src='"+img_page_o.src+"' width='17' height='21' border='0' align='absmiddle' />");
			else folderopen = "<img src='"+img_page_o.src+"' width='17' height='21' border='0' align='absmiddle' />";
			if(pCreate) folder = (isMozilla?"<img src='"+img_page_f.src+"' onmousedown=\"MydragStart('"+curID+"', event);\" onmouseup=\"MyDragEnd('"+curID+"', event);\" width='17' height='21' border='0' align='absmiddle' />":"<img ondrag=\"dragobj()\" ondragover=\"window.event.returnValue=false;\" ondrop=\"MyDragEnd('"+curID+"')\" ondragstart=\"MydragStart('"+curID+"')\" src='"+img_page_f.src+"' width='17' height='21' border='0' align='absmiddle' />");
			else folder = "<img src='"+img_page_f.src+"' width='17' height='21' border='0' align='absmiddle' />";
			if(pCreate) page = (isMozilla?"<img src='"+img_page_p.src+"' onmousedown=\"MydragStart('"+curID+"', event);\" onmouseup=\"MyDragEnd('"+curID+"', event);\" width='17' height='21' border='0' align='absmiddle' />":"<img ondrag=\"dragobj()\" ondragover=\"window.event.returnValue=false;\" ondrop=\"MyDragEnd('"+curID+"')\" ondragstart=\"MydragStart('"+curID+"')\" src='"+img_page_p.src+"' width='17' height='21' border='0' align='absmiddle' />");
			else page = "<img src='"+img_page_p.src+"' width='17' height='21' border='0' align='absmiddle' />";
		}else{
			if(pCreate) folderopen = (isMozilla?"<img src='"+img_page_o02.src+"' onmousedown=\"MydragStart('"+curID+"', event);\" onmouseup=\"MyDragEnd('"+curID+"', event);\" width='17' height='21' border='0' align='absmiddle' />":"<img ondrag=\"dragobj()\" ondragover=\"window.event.returnValue=false;\" ondrop=\"MyDragEnd('"+curID+"')\" ondragstart=\"MydragStart('"+curID+"')\" src='"+img_page_o02.src+"' width='17' height='21' border='0' align='absmiddle' />");
			else folderopen = "<img src='"+img_page_o.src+"' width='17' height='21' border='0' align='absmiddle' />";
			if(pCreate) folder = (isMozilla?"<img src='"+img_page_f02.src+"' onmousedown=\"MydragStart('"+curID+"', event);\" onmouseup=\"MyDragEnd('"+curID+"', event);\" width='17' height='21' border='0' align='absmiddle' />":"<img ondrag=\"dragobj()\" ondragover=\"window.event.returnValue=false;\" ondrop=\"MyDragEnd('"+curID+"')\" ondragstart=\"MydragStart('"+curID+"')\" src='"+img_page_f02.src+"' width='17' height='21' border='0' align='absmiddle' />");
			else folder = "<img src='"+img_page_f02.src+"' width='17' height='21' border='0' align='absmiddle' />";
			if(pCreate) page = (isMozilla?"<img src='"+img_page_p02.src+"' onmousedown=\"MydragStart('"+curID+"', event);\" onmouseup=\"MyDragEnd('"+curID+"', event);\" width='17' height='21' border='0' align='absmiddle' />":"<img ondrag=\"dragobj()\" ondragover=\"window.event.returnValue=false;\" ondrop=\"MyDragEnd('"+curID+"')\" ondragstart=\"MydragStart('"+curID+"')\" src='"+img_page_p02.src+"' width='17' height='21' border='0' align='absmiddle' />");
			else page = "<img src='"+img_page_p02.src+"' width='17' height='21' border='0' align='absmiddle' />";
		}

		if(curNode.hasChildNodes()) for(i=0;i<curNode.childNodes.length;i++)
		if(curNode.childNodes.item(i).nodeName=='treeitem') hasChildren=true;
		for(i=0;i<level;i++) result_str+=(levels[i]==1)?line:empty;
		result_str+=(visible=="true") ? (hasChildren?("<a href=# onClick=\"changeState('"+curID+"')\">"+((!lastNode)?minusbottom+"</a>"+folderopen:minus+"</a>"+folderopen)):((!lastNode)?joinbottom+page:join+page)) : (hasChildren?("<a href=# onClick=\"changeState('"+curID+"')\">"+((!lastNode)?plusbottom+"</a>"+folder:plus+"</a>"+folder)):((!lastNode)?joinbottom+page:join+page));
		result_str+="<span style=\"height: 10px;\"><a "+(curNode.getAttribute('var')!="virtual"?" href=\"<?=$http_path . ($MODREWRITE=="disabled"?"?vpath=/":"")?>"+getUrl(curID)+"\"":"")+" class=\"treeitem\">"+(isMozilla?childs.item(1).textContent:childs.item(0).text)+"</a></span></div>" + ((childsActions!=null)?("<div "+(ppCreate?" "+(isMozilla?"onmouseup=\"this.style.borderTop='0px'; move_node('"+curID+"');\" onmouseover=\"if(dragID) this.style.borderTop='1px solid black';\" onmouseout=\"this.style.borderTop='0px';\"":"onDrop=\"this.style.borderTop='0px'; move_node('"+curID+"');\" onDragOver=\"window.event.returnValue=false;\" onDragEnter=\"if(dragID) this.style.borderTop='1px solid black';\" onDragLeave=\"this.style.borderTop='0px';\"")+" ":"")+"style='border-bottom: solid 1px #B5D1E4; cursor: default'>&nbsp;</div></td>"):("</td>")) ;
		result_str+="<td width=\"0\" nowrap><nobr>&nbsp;&nbsp;&nbsp;";

		if(pCreate) result_str+="<a href=\"javascript:displayform('"+curID+"');\" style='color: #0277ED'><?=$lang["AddSubfolder"]?></a>";
		if(level!=0 && pWrite) result_str+=hline+ "<a href=\"javascript:displayform('"+curID+"', true);\" style='color: #0277ED'><?=$lang["doc_properties"]?></a>";
		result_str+="</td></tr></table>";
		if (visible=="true"){
			if (lastNode) levels[level]=1;
			level++;
			for(var i=0;i<childnodes;i++)
			if (childs.item(i).nodeName=="treeitem") viewNode(childs.item(i));
			level--;
			levels[level]=0;
		}
		result_str+="</div>\n";
	}
}


//определяет уровени для формивания levels

function cr_levels(nodeid){
	obj=docobj.nodeFromID(nodeid);
	level_in=level;
	while (obj.parentNode) {
		obj=obj.parentNode;
		level_in--;
		if(isMozilla && obj.nextSibling && obj.nextSibling.nodeName=="treeitem") var lastNode=true; else
		var lastNode = (isMozilla?!!(obj.nextSibling && obj.nextSibling.nodeName=="#text" && obj.nextSibling.nextSibling):(obj.nextSibling != null));

		if (lastNode) levels[level_in]=1;
		else levels[level_in]=0;
	}
}
// раскрытие/скрытие узла дерева

function changeState(nodeid){
	var curNode = docobj.nodeFromID(nodeid);
	if(isMozilla && curNode.nextSibling && curNode.nextSibling.nodeName=="treeitem") var lastNode=true; else
	var lastNode = (isMozilla?!!(curNode.nextSibling && curNode.nextSibling.nodeName=="#text" && curNode.nextSibling.nextSibling):(curNode.nextSibling != null));
	visible = (isMozilla?curNode.attributes.getNamedItem('visible').textContent:curNode.attributes.getNamedItem('visible').text);
	if(visible=='true')
	if(isMozilla) curNode.attributes.getNamedItem('visible').textContent='false'; else curNode.attributes.getNamedItem('visible').text='false';
	else
	if(isMozilla) curNode.attributes.getNamedItem('visible').textContent='true'; else curNode.attributes.getNamedItem('visible').text='true';


	result_str ="";
	level=levelObj(nodeid)-2;
	cr_levels(nodeid);
	viewNode(curNode);
	if(isMozilla) document.getElementById("tab_"+nodeid).innerHTML=result_str;
	else document.getElementById("tab_"+nodeid).outerHTML=result_str;
}

//уровень вложенности

function levelObj(nodeid){
	obj=docobj.nodeFromID(nodeid);
	var level=0;
	while (obj.parentNode!=null) {
		obj=obj.parentNode;
		level++;
	}
	return level;
}

//последний ли Node в узле

function lastInLevel(node){
	if (node.nextSibling != null)return true;
	else return false;
}

//если отпустить кнопку мыши (скрывает слой с перетаскиваемым объектом)

function clearSome(){
	document.getElementById('some').style.display="none";
	document.getElementById('some').innerHTML="";
	dragID=false;
}

//удаляет узел

function dropTrash(){
	if(isMozilla) docobj.normalize();
	if(!dragID) dragID=curnodeID;
	if (dragID && dragID!="id_0"){
		if (!recylceFull) recylceFull=dragID.replace("id_","");
		else recylceFull+=", "+dragID.replace("id_","");
		document.getElementById("recylceId").value=recylceFull;
		document.getElementById("recycle").src=recycle_full.src;
		document.getElementById("recycle").alt='Recycle Bin Full'+" "+recylceFull;
		document.getElementById('some').style.display="none";
		document.getElementById('some').innerHTML="";


		curNode=docobj.nodeFromID(dragID);
		if(isMozilla && curNode.nextSibling && curNode.nextSibling.nodeName=="treeitem") var lastNode=true; else
		var lastNode = (isMozilla?!!(curNode.nextSibling && curNode.nextSibling.nodeName=="#text" && curNode.nextSibling.nextSibling):(curNode.nextSibling != null));


		if (!lastNode) {
			if(isMozilla){
				if(curNode.previousSibling && curNode.previousSibling.nodeName=="#text" && curNode.previousSibling.previousSibling && curNode.previousSibling.previousSibling.attributes.getNamedItem("id"))
				prefID=curNode.previousSibling.previousSibling.attributes.getNamedItem("id").textContent;
				else prefID = null;
			}else{
				if (docobj.nodeFromID(dragID).previousSibling.attributes.getNamedItem('id') != null)
				prefID=docobj.nodeFromID(dragID).previousSibling.attributes.getNamedItem('id').text;
				else prefID = null;
			}
		}
		docobj.nodeFromID(dragID).parentNode.removeChild(docobj.nodeFromID(dragID));
		document.getElementById("tab_"+dragID).outerHTML="";
		printElements();
	}
	dragID=false;
}

//проверка является ли источник элементом исходного узла

function isInTree(source,dest){
	obj=docobj.nodeFromID(dest);
	sourceObj=docobj.nodeFromID(source);
	flag=false;
	while (obj.parentNode!=null && !flag) {
		if (sourceObj==obj) flag=true;
		obj=obj.parentNode;
	}
	return flag;
}

//Замена Id у всех потомков нового узла

function Change_Id(curNode){
	var childs = curNode.childNodes;
	var childnodes = childs.length;
	max_id++;
	curNode.setAttribute("id","id_"+max_id);
	for(var i=0;i<childnodes;i++)
	if (childs.item(i).nodeName=="treeitem") Change_Id(childs.item(i));
}

//Замена Id у всех потомков нового узла на указатели

function Change_Pointer_Id(curNode){
	var childs = curNode.childNodes;
	var childnodes = childs.length;
	max_id++;
	curNode.setAttribute("id","id_"+max_id);
	for(var i=0;i<childnodes;i++)
	if (childs.item(i).nodeName=="treeitem") Change_Pointer_Id(childs.item(i));
}

// происходит при событии Drop

function MyDragEnd(nodeID, ev){

	document.getElementById('some').style.display="none";
	document.getElementById('some').innerHTML="";
	if(isIE) var clipBoard=clipboardData.getData("Text"); else var clipBoard="";
	if (clipBoard!="" && window.event.shiftKey){
		var xml_transfer = new ActiveXObject("Msxml2.DOMDocument");
		xml_transfer.async = false;
		xml_transfer.resolveExternals = false;
		if (xml_transfer.loadXML(clipBoard)){
			clipboardData.setData("Text","");
			xml_transfer=xml_transfer.firstChild;
			Change_Pointer_Id(xml_transfer);
			docobj.nodeFromID(nodeID).insertBefore(xml_transfer,null);
			docobj.nodeFromID(nodeID).attributes.getNamedItem('visible').text='true';
			printElements();
		}
	} else {

		if (dragID){
			if (dragID!=nodeID && !isInTree(dragID,nodeID)) {

				var curNode=docobj.nodeFromID(dragID);
				if(isMozilla && curNode.nextSibling && curNode.nextSibling.nodeName=="treeitem") var lastNode=true; else
				var lastNode = (isMozilla?!!(curNode.nextSibling && curNode.nextSibling.nodeName=="#text" && curNode.nextSibling.nextSibling):(curNode.nextSibling != null));

				if (window.event.ctrlKey){
					Change_Id(copyNode);
					docobj.nodeFromID(nodeID).insertBefore(copyNode,null);
					if (!lastNode) printElements();
				} else {
					docobj.nodeFromID(nodeID).insertBefore(docobj.nodeFromID(dragID),null);
					if (!lastNode) printElements();
					else {
						if(isMozilla) document.getElementById("tab_"+dragID).innerHTML=""; else document.getElementById("tab_"+dragID).outerHTML="";
					}
				}
			}

			if(docobj.nodeFromID(nodeID).attributes.getNamedItem('visible').text=='false') {
				docobj.nodeFromID(nodeID).attributes.getNamedItem('visible').text='true';
			}
			result_str ="";
			level=levelObj(nodeID)-2;
			cr_levels(nodeID);
			viewNode(docobj.nodeFromID(nodeID));

			if(isMozilla) {
				document.getElementById("tab_"+nodeID).innerHTML=result_str;
			} else document.getElementById("tab_"+nodeID).outerHTML=result_str;
			dragID=false;
		}
	}

}

// изменяет координаты объекта DRAG&DROP

function dragobj(ev){
	if(ev) event=ev;
	if( document.getElementById('some').innerHTML != ""){
		document.getElementById('some').style.left = event.clientX-X/2 + document.body.scrollLeft;
		document.getElementById('some').style.top = event.clientY-Y/2 + document.body.scrollTop;
	}
}

//отображает объект во время DRAG&DROP

function viewDragNode(curNode){
	var childnodes = curNode.childNodes.length;
	if(isMozilla && curNode.nextSibling && curNode.nextSibling.nodeName=="treeitem") var lastNode=true; else
	var lastNode = (isMozilla?!!(curNode.nextSibling && curNode.nextSibling.nodeName=="#text" && curNode.nextSibling.nextSibling):(curNode.nextSibling != null));
	var curID = (curNode.attributes.getNamedItem('id'))?(isMozilla?curNode.attributes.getNamedItem('id').textContent:curNode.attributes.getNamedItem('id').text):"";
	if (curNode.nodeName=="treeitem"){
		folder		= "<img src='"+img_page_f.src+"' width='17' height='21' border='0' align='absmiddle' />";
		page		= "<img src='"+img_page_p.src+"' width='17' height='21' border='0' align='absmiddle' />";

		Notifier="";
		if (window.event.ctrlKey) Notifier=' <span style="color: blue">(<?=$lang["ieeditor"]["Copy"]?>)</span>';

		result_str+="<tr style=\"display:''\"><td width=100% nowrap id="+curID+">";
		result_str+=(curNode.lastChild.nodeName=='treeitem')?(folder):(page);
		result_str+="<span style=\"cursor:hand; color: Black;\">"+(isMozilla?curNode.childNodes.item(1).textContent:curNode.childNodes.item(0).text)+"</span>"+Notifier+"</td>";
		result_str+="</tr>";
	}
}



//возникает при попытке перетащить объект

function MydragStart(nodeID, ev){

	if (ev) {
		event=ev;
		window.addEventListener("mousemove", dragobj, false);
		X = -40;
		Y = -20;
		event.preventDefault();
	}else{
		X = -40;
		Y = -20;
	}

	dragID=nodeID;
	if (isIE && window.event.shiftKey){
		var xml_transfer = new ActiveXObject("Microsoft.XMLDOM");
		xml_transfer.appendChild(docobj.nodeFromID(dragID).cloneNode(true));
		clipboardData.setData("Text",xml_transfer.xml);
	}

	copyNode=docobj.nodeFromID(dragID).cloneNode(true);
	document.getElementById('some').zIndex="100";

	document.getElementById('some').style.left = event.clientX-X/2 + document.body.scrollLeft;
	document.getElementById('some').style.top = event.clientY-Y/2 + document.body.scrollTop;
	document.getElementById('some').style.display="";
	event.returnValue = true;

	levels = new Array()
	result_str="<div class=table style=\"cursor: hand; color: #CCCCCC; \">";
	for(i=0; i<20; i++) levels[i]=0;
	level=0;
	viewDragNode(docobj.nodeFromID(nodeID));
	result_str+="</div>";
	document.getElementById('some').innerHTML = result_str;
}



//перемещение узла на позицию вверх в данном поддереве

function gettingUp(nodeid){
	if(!nodeid) nodeid=curnodeID;
	if (docobj.nodeFromID(nodeid).previousSibling.nodeName=='treeitem' || docobj.nodeFromID(nodeid).previousSibling.previousSibling.nodeName=="treeitem"){

		prefID=(docobj.nodeFromID(nodeid).previousSibling.nodeName=='treeitem'?docobj.nodeFromID(nodeid).previousSibling.attributes.getNamedItem('id').text:docobj.nodeFromID(nodeid).previousSibling.previousSibling.attributes.getNamedItem('id').text);
		docobj.nodeFromID(nodeid).parentNode.insertBefore(docobj.nodeFromID(nodeid),docobj.nodeFromID(prefID));
		var curNode = docobj.nodeFromID(nodeid).parentNode;
		nodeid = curNode.attributes.getNamedItem('id').text;
		result_str ="";
		level=levelObj(nodeid)-2;
		cr_levels(nodeid);
		viewNode(curNode);
		if(isMozilla) document.getElementById("tab_"+nodeid).innerHTML=result_str; else document.getElementById("tab_"+nodeid).outerHTML=result_str;

	}
}

//перемещение узла на позицию вниз в данном поддереве

function gettingDown(nodeid){
	if(!nodeid) nodeid=curnodeID;
	if (docobj.nodeFromID(nodeid).nextSibling!=null && (docobj.nodeFromID(nodeid).nextSibling.nodeName=="#text"?docobj.nodeFromID(nodeid).nextSibling.nextSibling:true)){
		gettingUp((docobj.nodeFromID(nodeid).nextSibling.nodeName=="#text"?docobj.nodeFromID(nodeid).nextSibling.nextSibling.attributes.getNamedItem('id').text:docobj.nodeFromID(nodeid).nextSibling.attributes.getNamedItem('id').text));
	}
}


function getUrl(nodeID){
	var nodeVar=(isMozilla?docobj.getElementById(nodeID).getAttribute('var'):docobj.nodeFromID(nodeID).getAttribute('var'));
	var path="";
	if (nodeVar!="root") {
		if((isMozilla?docobj.getElementById(nodeID).parentNode.getAttribute('id'):docobj.nodeFromID(nodeID).parentNode.getAttribute('id'))){
			path=getUrl((isMozilla?docobj.getElementById(nodeID).parentNode.getAttribute('id'):docobj.nodeFromID(nodeID).parentNode.getAttribute('id')))+nodeVar + "/";
		}
	}
	if(path!="root/")
	return path; else return "";
}

function move_node(before_id){

	if(before_id!="id_0" && dragID!=0 && !isInTree(dragID, before_id)){
		if(isMozilla)
		docobj.getElementById(before_id).parentNode.insertBefore(docobj.getElementById(dragID),docobj.getElementById(before_id));
		else
		docobj.nodeFromID(before_id).parentNode.insertBefore(docobj.nodeFromID(dragID),docobj.nodeFromID(before_id));
		printElements();
	}
	clearSome();
}


function displayform(nodeID, props){
	if(!nodeID) nodeID=curnodeID;
	if(nodeID=="current") nodeID=curnodeID;
	document.getElementById("mask").value="";
	document.getElementById("title").value="New Page";
	document.getElementById("var").value="newpage";
	if(props) {
		in_props=true;
		curnodeID=nodeID;
		document.getElementById("docid").innerHTML=nodeID.substr(3,10);
		str=getUrl((isMozilla?docobj.getElementById(nodeID).parentNode.getAttribute('id'):docobj.nodeFromID(nodeID).parentNode.getAttribute('id')));
		if(str.length>=50){
			diff=str.length-50;
			document.getElementById('url').innerHTML='<?=$http_path?>&nbsp;...&nbsp;'+str.substring(diff);
		}else document.getElementById('url').innerHTML='<?=$http_path?>'+str;

			document.getElementById('var').value=docobj.nodeFromID(nodeID).getAttribute('var');
			document.getElementById("owner").value=docobj.nodeFromID(nodeID).getAttribute('owner');
			document.getElementById("group").value=docobj.nodeFromID(nodeID).getAttribute('group');
			document.getElementById("permissions").value=docobj.nodeFromID(nodeID).getAttribute('permissions');
			checkPermissions(docobj.nodeFromID(nodeID));
			displayPermissions();

		if(User!=(isMozilla?docobj.getElementById(nodeID).getAttribute('owner'):docobj.nodeFromID(nodeID).getAttribute('owner')) && User!='<?=$admin_login?>'){
			document.getElementById("owner").readOnly=true;
			document.getElementById("group").readOnly=true;
			document.getElementById("permissions").readOnly=true;
		} else {
			document.getElementById("owner").readOnly=false;
			document.getElementById("group").readOnly=false;
			document.getElementById("permissions").readOnly=false;
		}

		document.getElementById("title").value=docobj.nodeFromID(nodeID).childNodes.item((isMozilla?1:0)).text;
		for (i=0; i<(isMozilla?docobj.getElementById(nodeID).childNodes.length:docobj.nodeFromID(nodeID).childNodes.length); i++){
			if(isMozilla){
				if(docobj.getElementById(nodeID).childNodes.item(i).nodeName=="mask") {
					document.getElementById("mask").value=docobj.getElementById(nodeID).childNodes.item(i).textContent;
				}
				if(docobj.getElementById(nodeID).childNodes.item(i).nodeName=="title") {
					document.getElementById("title").value=docobj.getElementById(nodeID).childNodes.item(i).textContent;
				}
				if(docobj.getElementById(nodeID).childNodes.item(i).nodeName=="template") {
					for (j=0; j<document.getElementById('template').options.length; j++){
						if(docobj.getElementById(nodeID).childNodes.item(i).text==document.getElementById('template').options.item(j).textContent)
						document.getElementById("template").options.item(j).selected=true;
					}
				}
			}else{
				if(docobj.nodeFromID(nodeID).childNodes.item(i).nodeName=="mask") {
					document.getElementById("mask").value=docobj.nodeFromID(nodeID).childNodes.item(i).text;
				}
				if(docobj.nodeFromID(nodeID).childNodes.item(i).nodeName=="template") {
					for (j=0; j<document.getElementById('template').options.length; j++){
						if(docobj.nodeFromID(nodeID).childNodes.item(i).text==document.getElementById('template').options.item(j).text)
						document.getElementById("template").options.item(j).selected=true;
					}
				}
			}

		}

	}else {
		document.getElementById("docid").innerHTML="to be generated...";
		in_props=false;
		str=getUrl(nodeID);
		if(str.length>=50){
			diff=str.length-50;
			document.getElementById('url').innerHTML='<?=$http_path?>&nbsp;...&nbsp;'+str.substring(diff);
		}else document.getElementById('url').innerHTML='<?=$http_path?>'+str;

		document.getElementById("owner").value=User;
		document.getElementById("group").value=Group;
		document.getElementById("permissions").value=755;

	}

	document.getElementById('panel').style.display="block";
	document.getElementById('panel').style.top=document.body.scrollTop+200;
	document.getElementById('title').style.width=document.getElementById('variable').offsetWidth;
	document.getElementById('mask').style.width=document.getElementById('variable').offsetWidth-122;
	document.getElementById('template').style.width=document.getElementById('variable').offsetWidth;
	document.getElementById('owner').style.width=document.getElementById('variable').offsetWidth;
	document.getElementById('group').style.width=document.getElementById('variable').offsetWidth;
	document.getElementById('permissions').style.width=document.getElementById('variable').offsetWidth;


	document.getElementById('panel').style.width=document.getElementById('variable').offsetWidth+250;
	document.getElementById('header').style.width=document.getElementById('variable').offsetWidth+230;
	curnodeID=nodeID;


}

function start_track(ev){
	if (ev) {
		event=ev;
		window.addEventListener("mousemove", track, false);
		X = 2*event.layerX;
		Y = 2*event.layerY;
		event.preventDefault();
	}else{
		X = event.offsetX+event.x;
		Y = event.offsetY+event.y;
	}
	drag=true;
	return false;
}


function stop_track(){
	if(isMozilla) {
		window.removeEventListener("mousemove", track, false);
	}
	drag=false;
	ox=0;
	oy=0;
	return false;
}

function track(ev){
	if (ev) event=ev;

	if(drag) {
		document.getElementById("panel").style.left = event.clientX-X/2 + document.body.scrollLeft;
		document.getElementById("panel").style.top = event.clientY-Y/2 + document.body.scrollTop;
	}
	return true;
}

function insertPage(){
	nodeID=curnodeID;
	if(in_props){
		if(isMozilla){
			docobj.getElementById(nodeID).setAttribute("var", document.getElementById('var').value);
			docobj.getElementById(nodeID).setAttribute("owner", document.getElementById('owner').value);
			docobj.getElementById(nodeID).setAttribute("group", document.getElementById('group').value);
			docobj.getElementById(nodeID).setAttribute("permissions", document.getElementById('permissions').value);

			mask_set=false;
			for (i=0; i<docobj.getElementById(nodeID).childNodes.length; i++){
				if(docobj.getElementById(nodeID).childNodes.item(i).nodeName=="mask") mask_set=true;
				if(docobj.getElementById(nodeID).childNodes.item(i).nodeName!="template" && docobj.getElementById(nodeID).childNodes.item(i).nodeName!="treeitem" && docobj.getElementById(nodeID).childNodes.item(i).nodeName!="#text") {
					docobj.getElementById(nodeID).childNodes.item(i).textContent=document.getElementById(docobj.getElementById(nodeID).childNodes.item(i).nodeName).value;
				}
				if(docobj.getElementById(nodeID).childNodes.item(i).nodeName=="template") {
					docobj.getElementById(nodeID).childNodes.item(i).textContent=document.getElementById("template").options[document.getElementById("template").selectedIndex].value;
				}

			}
			if (!mask_set && document.getElementById("mask").value!=""){
				elem=docobj.createElement('mask');
				elem.text=document.getElementById("mask").value;
				docobj.getElementById(nodeID).insertBefore(elem, docobj.getElementById(nodeID).childNodes.item(1));
			}
			curNode=docobj.getElementById(nodeID);
		}else{
			docobj.nodeFromID(nodeID).setAttribute("var", document.getElementById('var').value);
			docobj.nodeFromID(nodeID).setAttribute("owner", document.getElementById('owner').value);
			docobj.nodeFromID(nodeID).setAttribute("group", document.getElementById('group').value);
			docobj.nodeFromID(nodeID).setAttribute("permissions", document.getElementById('permissions').value);
			mask_set=false;
			for (i=0; i<docobj.nodeFromID(nodeID).childNodes.length; i++){
				if(docobj.nodeFromID(nodeID).childNodes.item(i).nodeName=="mask") mask_set=true;
				if(docobj.nodeFromID(nodeID).childNodes.item(i).nodeName!="template" && docobj.nodeFromID(nodeID).childNodes.item(i).nodeName!="treeitem") {
					docobj.nodeFromID(nodeID).childNodes.item(i).text=document.getElementById(docobj.nodeFromID(nodeID).childNodes.item(i).nodeName).value;
				}
				if(docobj.nodeFromID(nodeID).childNodes.item(i).nodeName=="template") {
					docobj.nodeFromID(nodeID).childNodes.item(i).text=document.getElementById("template").options[document.getElementById("template").selectedIndex].value;
				}

			}
			if (!mask_set && document.getElementById("mask").value!=""){
				elem=docobj.createElement('mask');
				elem.text=document.getElementById("mask").value;
				docobj.nodeFromID(nodeID).insertBefore(elem, docobj.nodeFromID(nodeID).childNodes.item(1));
			}
			curNode=docobj.nodeFromID(nodeID);
		}


		result_str ="";
		level=levelObj(nodeID)-2;
		cr_levels(nodeID);
		viewNode(curNode);
		if(isMozilla) document.getElementById("tab_"+nodeID).innerHTML=result_str; else document.getElementById("tab_"+nodeID).outerHTML=result_str;
	}else{
		new_id=max_id+1;

		var txml = '\n<treeitem var="'+document.getElementById("var").value+'" id="id_'+new_id+'" visible="true" owner="'+document.getElementById("owner").value+'" group="'+document.getElementById("group").value+'" permissions="'+document.getElementById("permissions").value+'">\n<title>'+document.getElementById("title").value+'</title>\n<mask>'+document.getElementById("mask").value+'</mask>\n<template>'+document.getElementById("template").options[document.getElementById("template").selectedIndex].value+'</template>\n</treeitem>\n';

		if(isMozilla) new_page=document.implementation.createDocument("", "tmp", null); else var new_page = new ActiveXObject("Msxml2.DOMDocument");
		new_page.loadXML(txml);
		docobj.nodeFromID(nodeID).appendChild(new_page.firstChild);


		printElements();
	}
	document.getElementById('panel').style.display="none";

	if(isMozilla) {
		document.getElementById('new_xml').value=docobj.xml;
		do_quick_save('tree_form');
	} else q_save();


}

function q_save(){
	quick_save.document.write('<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"></HEAD<BODY><DIV style="visibility: hidden">');
	quick_save.document.write("<form method=\"post\" id=\"tree_form\" action=\"<?=$http_path?>\"><textarea name=\"post_site[new_xml]\">"+docobj.xml+"</textarea><input type=\"hidden\" name=\"quick_save\" value=\"true\" /></FORM>");
	quick_save.document.write('</DIV></BODY></HTML>');
	quick_save.document.close();
	quick_save.document.charset="<?=$default_charset?>";
	quick_save.document.getElementById('tree_form').submit();
}



function hidemenu(){
	document.getElementById('menu').style.visibility="hidden";
}

function showmenu(id, evt){
	if(isMozilla) event=evt;
	curnodeID=id;
	var rightedge = document.body.clientWidth-event.clientX;
	var bottomedge = document.body.clientHeight-event.clientY;
	if (rightedge < document.getElementById('menu').offsetWidth)
	document.getElementById('menu').style.left = document.body.scrollLeft + event.clientX - document.getElementById('menu').offsetWidth;
	else
	document.getElementById('menu').style.left = document.body.scrollLeft + event.clientX;
	if (bottomedge < document.getElementById('menu').offsetHeight)
	document.getElementById('menu').style.top = document.body.scrollTop + event.clientY - document.getElementById('menu').offsetHeight;
	else
	document.getElementById('menu').style.top = document.body.scrollTop + event.clientY;
	document.getElementById('menu').style.visibility = "visible";
	return false;
}

function highlight(evt) {
	if(isMozilla) {
		event=evt;
		var srcElement=event.target;
	}else var srcElement=event.srcElement;

	if (srcElement.className == "item") {
		srcElement.style.backgroundColor = "#CEE4F2";
	}
}

function lowlight(evt) {
	if(isMozilla) {
		event=evt;
		var srcElement=event.target;
	}else var srcElement=event.srcElement;

	if (srcElement.className == "item") {
		srcElement.style.backgroundColor = "";
		window.status = "";
	}
}

function jumpto(evt) {
	if(isMozilla) {
		event=evt;
		var srcElement=event.target;
	}else var srcElement=event.srcElement;
	if (srcElement.className == "item") {
		if (srcElement.getAttribute("target") != null)
		window.open(srcElement.getAttribute("url"), srcElement.getAttribute("target"));
		else
		window.location = srcElement.getAttribute("url");
	}
	hidemenu();
}

function view(){
	window.open("<?=$http_path?>"+getUrl(curnodeID), "_blank");
}

function checkPermissions(Node){
	pRead=false;
	pWrite=false;
	pCreate=false;
	if(User==Node.getAttribute('owner') || User=='<?=$admin_login?>'){
		pRead=true;
		pWrite=true;
		pCreate=true;
	}else{
		perms=Node.getAttribute('permissions');
		if(perms!=null){
			if(Group==Node.getAttribute('group')){
				pCurrent=perms.substr(1,1);
			}else{
				pCurrent=perms.substr(2,1);
			}

			if(pCurrent>=4) pRead=true; else pRead=false;
			if(pCurrent>=6) pWrite=true; else pWrite=false;
			if(pCurrent==7) pCreate=true; else pCreate=false;
		}

	}


	return true;
}

function displayPermissions(){
	var perms=document.getElementById('permissions').value;

	if(!pCreate){
		document.getElementById('p11').disabled=true;
		document.getElementById('p21').disabled=true;
		document.getElementById('p31').disabled=true;
		document.getElementById('p41').disabled=true;

		document.getElementById('p12').disabled=true;
		document.getElementById('p22').disabled=true;
		document.getElementById('p32').disabled=true;
		document.getElementById('p42').disabled=true;

		document.getElementById('p13').disabled=true;
		document.getElementById('p23').disabled=true;
		document.getElementById('p33').disabled=true;
		document.getElementById('p43').disabled=true;

		document.getElementById('permissions').disabled=true;

	}else{

		document.getElementById('p11').disabled=false;
		document.getElementById('p21').disabled=false;
		document.getElementById('p31').disabled=false;
		document.getElementById('p41').disabled=false;

		document.getElementById('p12').disabled=false;
		document.getElementById('p22').disabled=false;
		document.getElementById('p32').disabled=false;
		document.getElementById('p42').disabled=false;

		document.getElementById('p13').disabled=false;
		document.getElementById('p23').disabled=false;
		document.getElementById('p33').disabled=false;
		document.getElementById('p43').disabled=false;

		document.getElementById('permissions').disabled=false;
	}


	if(perms!=null){
		pOwner=parseInt(perms.substr(0,1));
		pGroup=parseInt(perms.substr(1,1));
		pOthers=parseInt(perms.substr(2,1));

		document.getElementById('p11').checked=false;
		document.getElementById('p21').checked=false;
		document.getElementById('p31').checked=false;
		document.getElementById('p41').checked=false;

		document.getElementById('p12').checked=false;
		document.getElementById('p22').checked=false;
		document.getElementById('p32').checked=false;
		document.getElementById('p42').checked=false;

		document.getElementById('p13').checked=false;
		document.getElementById('p23').checked=false;
		document.getElementById('p33').checked=false;
		document.getElementById('p43').checked=false;


		if(pOwner>=4) document.getElementById('p41').checked=true;
		if(pOwner>=5) document.getElementById('p31').checked=true;
		if(pOwner>=6) document.getElementById('p21').checked=true;
		if(pOwner>=7) document.getElementById('p11').checked=true;

		if(pGroup>=4) document.getElementById('p42').checked=true;
		if(pGroup>=5) document.getElementById('p32').checked=true;
		if(pGroup>=6) document.getElementById('p22').checked=true;
		if(pGroup>=7) document.getElementById('p12').checked=true;

		if(pOthers>=4) document.getElementById('p43').checked=true;
		if(pOthers>=5) document.getElementById('p33').checked=true;
		if(pOthers>=6) document.getElementById('p23').checked=true;
		if(pOthers>=7) document.getElementById('p13').checked=true;
	}
}

function perms2Int(){

	if(pCreate){
		var pOwner=0;
		if(document.getElementById('p41').checked==true) pOwner=4;
		if(document.getElementById('p31').checked==true) pOwner=5;
		if(document.getElementById('p21').checked==true) pOwner=6;
		if(document.getElementById('p11').checked==true) pOwner=7;

		var pGroup=0;
		if(document.getElementById('p42').checked==true) pGroup=4;
		if(document.getElementById('p32').checked==true) pGroup=5;
		if(document.getElementById('p22').checked==true) pGroup=6;
		if(document.getElementById('p12').checked==true) pGroup=7;

		var pOthers=0;
		if(document.getElementById('p43').checked==true) pOthers=4;
		if(document.getElementById('p33').checked==true) pOthers=5;
		if(document.getElementById('p23').checked==true) pOthers=6;
		if(document.getElementById('p13').checked==true) pOthers=7;

		var perms=pOwner + '' + pGroup + '' + pOthers;

		document.getElementById('permissions').value=perms;
		displayPermissions();
	}
}

function checkvariable(type, ev){
	var Code=(isMozilla?ev.charCode:window.event.keyCode);

	if(type=="var"){
		if ( (Code >= 32 && Code < 48) || (Code > 57 && Code < 65) || (Code > 90 && Code < 95)) return false;
	}

	if(type=="digit"){
		if ( Code < 48 || Code > 57) return false;
	}
	return true;
}

function viewObject(name)
{
	var obj = eval(name), i;
	if(!obj) {
		alert("\""+name+"\" ia not an object");
		return;
	}
	var w_Test = open("","Test","width=600,height=500,scrollbars=1");
	if(!w_Test)	{
		alert("Cannot open window for "+name);
		return;
	}

	w_Test.document.open();

	for(i in obj) w_Test.document.write(name+"."+i+"="+obj[i]+"<br>");

	w_Test.document.close();
}